'''
读文件相关操作
'''
import configparser
import os

import yaml


def get_project_path():
    '''
    获取当前工程路径
    :return:
    '''
    cf = os.path.realpath(__file__)  # 获取当前文件路径 fileread.py
    cd = os.path.dirname(cf)  # caw
    cd = os.path.dirname(cd)  # zonghe
    return cd + "\\"


def read_ini(path, key):
    '''
    读配置文件
    :param path:  文件路径
    :param key:  配置文件中的key
    :return: 返回key对应的value
    '''
    # configparser python中读取配置文件的模块

    path = get_project_path() + path
    c = configparser.ConfigParser()
    c.read(path)
    value = c.get("env", key)  # [env] section
    return value


def read_yaml(path):
    '''
    读取测试数据文件
    :param path: 文件路径
    :return: 文件内容
    '''

    path = get_project_path() + path
    with open(path, mode="r", encoding='utf-8') as f:
        content = f.read()
        ret = yaml.load(content, yaml.FullLoader)  #yaml.FullLoader全部加载文件
        return ret


if __name__ == '__main__':
    print(read_yaml(r'data_case\register_fail.yaml'))

    print(get_project_path())  # C:\ApiAuto Test\zonghe\
    u = read_ini(r"data_env\env.ini", "url")
    print(u)
    d = read_ini(r"data_env\env.ini", "db")
    print(d)
    print(type(d))

    # 字符串转成字典

    dd = eval(d)
    print(type(dd))
    print(dd)
